﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class OrganizationUnitService : BaseService, IOrganizationUnitService
    {
        private readonly IOrganizationUnitRepository _organizationUnit;
        private readonly IUnitOfWork unitOfWork;
        public OrganizationUnitService(IMapper mapper, IUnitOfWork unitOfWork, IOrganizationUnitRepository organizationUnit) : base(mapper)
        {

            this.unitOfWork = unitOfWork;
            _organizationUnit = organizationUnit;
        }

        public bool AddOrModefiyOrganizationUnit(OrganizationUnit model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _organizationUnit.UpdEntity(model);
            }
            else
            {
                _organizationUnit.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }

        public bool DelOrganizationUnit(long organizationUnitId)
        {
            var Quote = _organizationUnit.QueryEntityById(organizationUnitId);
            if (Quote == null)
            {
                return false;
            }
            _organizationUnit.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }

        public OrganizationUnit? GetOrganizationUnit(long organizationUnitId)
        {
            return _organizationUnit.QueryEntityById(organizationUnitId);
        }

        public List<OrganizationUnit> GetOrganizationUnitData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _organizationUnit.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.Name.Contains(name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
    }
}
